﻿using Dyao.Repository.Abstractionsx;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Entity;
using Foundation.Uow;

namespace Dyao.Book.Application
{
    public class TaskCenterRecordLogApplication : BaseApplication, ITaskCenterRecordLogApplication
    {
        private readonly ITaskCenterRecordLogRepository _taskCenterRecordLogRepository;

        public TaskCenterRecordLogApplication(ITaskCenterRecordLogRepository TaskCenterRecordLogRepository)
        {
            _taskCenterRecordLogRepository = TaskCenterRecordLogRepository;
        }

        /// <summary>
        /// 添加记录日志
        /// </summary>
        /// <param name="recordLogModel">recordLogModel</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns></returns>
        public async Task<bool> PostTaskCenterRecordLogAsync(TaskCenterRecordLogDto recordLogModel, CancellationToken cancellationToken = default)
        {
            using (var unitOfWork = UnitOfWorkManager.Begin(true))
            {
                TaskCenterRecordLogEntity taskCenterRecordLog = ObjectMapper.Map<TaskCenterRecordLogDto, TaskCenterRecordLogEntity>(recordLogModel);
                taskCenterRecordLog = await _taskCenterRecordLogRepository.InsertAsync(taskCenterRecordLog, false, cancellationToken);
                await unitOfWork.CompleteAsync(cancellationToken);
                return true;
            }
        }

        /// <summary>
        /// 批量添加记录日志
        /// </summary>
        /// <param name="logs"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task InsertAsync(IEnumerable<TaskCenterRecordLogDto> logs, CancellationToken cancellationToken = default)
        {
            if (logs == null || logs.Count() == 0) return;
            using (var unitOfWork = UnitOfWorkManager.Begin(true))
            {
                var entities = new List<TaskCenterRecordLogEntity>(logs.Count());
                foreach (var item in logs)
                {
                    var entity = ObjectMapper.Map<TaskCenterRecordLogDto, TaskCenterRecordLogEntity>(item);
                    entities.Add(entity);
                }

                await _taskCenterRecordLogRepository.InsertRangeAsync(entities, false, cancellationToken);

                await unitOfWork.CompleteAsync(cancellationToken);
            }
        }
    }
}
